﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DHQGHN.DAL.Common;
using DHQGHN.DAL.DAO.Admin;
using DHQGHN.DAL.Models.Admin;

namespace DHQGHN.DAL.BO.Admin
{
    public class ApplicationBO: BaseBO
    {
        public const string FIELD_ID = "AppId";
        public const string FIELD_APP_NAME = "AppName";
        public const string FIELD_APP_CODE = "AppCode";
        public const string FIELD_APP_DESCRIPTION = "AppDescription";
        public const string FIELD_IS_ACTIVE = "IsActive";
        public const string Field_RECORD_COUNT = "RecordCount";      

        private String _connectionString;
        
        public ApplicationBO(string connString)
        {
            this._connectionString = connString;

        }
        public ApplicationBO()
        {
            this._connectionString = Common.ConnectionUtil.DEFAULT_CONN_STRING;

        }

        public ApplicationDAO _appDAO;

        public ApplicationDAO CreateSQLDAO()
        {
            return new ApplicationDAO(_connectionString, DEFAULT_MAPPER_TYPE);
        }

        public DataPage<ApplicationModel> GetDynamicListPaging(ApplicationModel aModel, int pageSize, int pageIndex)
        {
            try
            {
                _appDAO = CreateSQLDAO();
                Dictionary<String, String> mapDictionary = new Dictionary<string, string>()
                {
                    {ApplicationModel.FIELD_ID,FIELD_ID},
                    {ApplicationModel.FIELD_APP_NAME,FIELD_APP_NAME},
                    {ApplicationModel.FIELD_APP_CODE,FIELD_APP_CODE},  
                    {ApplicationModel.FIELD_APP_DESCRIPTION,FIELD_APP_DESCRIPTION},
                    {ApplicationModel.FIELD_IS_ACTIVE,FIELD_IS_ACTIVE},                    
                    {ApplicationModel.FIELD_RECORD_COUNT,Field_RECORD_COUNT}
                    
                };
                return _appDAO.GetDynamicListPaging(aModel, pageSize, pageIndex, mapDictionary);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public ApplicationModel GetAppById(int? appId)
        {
            try
            {
                _appDAO = CreateSQLDAO();

                Dictionary<String, String> mapDictionary = new Dictionary<string, string>()
                {
                    {ApplicationModel.FIELD_ID,FIELD_ID},
                    {ApplicationModel.FIELD_APP_NAME,FIELD_APP_NAME},
                    {ApplicationModel.FIELD_APP_CODE,FIELD_APP_CODE},  
                    {ApplicationModel.FIELD_APP_DESCRIPTION,FIELD_APP_DESCRIPTION},
                    {ApplicationModel.FIELD_IS_ACTIVE,FIELD_IS_ACTIVE}
                    
                };

                return _appDAO.GetById(appId, mapDictionary);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public ApplicationModel GetByCode(string code)
        {
            try
            {
                Dictionary<String, String> mapDictionary = new Dictionary<string, string>()
                {
                    {ApplicationModel.FIELD_ID,FIELD_ID},
                    {ApplicationModel.FIELD_APP_NAME,FIELD_APP_NAME},
                    {ApplicationModel.FIELD_APP_CODE,FIELD_APP_CODE},  
                    {ApplicationModel.FIELD_APP_DESCRIPTION,FIELD_APP_DESCRIPTION},
                    {ApplicationModel.FIELD_IS_ACTIVE,FIELD_IS_ACTIVE}
                    
                };
                _appDAO = CreateSQLDAO();
                return _appDAO.GetByCode(code, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        public bool createOrUpdateApp(ApplicationModel appModel)
        {
            try {
                _appDAO = CreateSQLDAO();
                return _appDAO.createOrUpdateApp(appModel);
            }
            catch (Exception e)
            {
                return false;
            }            
        }

        public bool deleteApp(int[] appId)
        {
            try
            {
                _appDAO = CreateSQLDAO();
                return _appDAO.deleteApp(appId);
            }
            catch (Exception e)
            {
                return false;
            }   
        }

        public List<ApplicationModel> GetAll()
        {
            try
            {
                _appDAO = CreateSQLDAO();
                Dictionary<String, String> mapDictionary = new Dictionary<string, string>()
                {
                    {ApplicationModel.FIELD_ID,FIELD_ID},
                    {ApplicationModel.FIELD_APP_NAME,FIELD_APP_NAME},
                    {ApplicationModel.FIELD_APP_CODE,FIELD_APP_CODE}     
                };
                return _appDAO.GetAll(mapDictionary);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public bool checkPermissionOfUser(int userId, string appCode)
        {
            try
            {
                _appDAO = CreateSQLDAO();
                return _appDAO.checkPermissionOfUser(userId, appCode);
            }
            catch (Exception e)
            {
                return false;
            }            
        }

        public bool checkAppExist(int appId, string appCode, string appName)
        {
            try
            {
                _appDAO = CreateSQLDAO();
                return _appDAO.checkAppExist(appId, appCode, appName);
            }
            catch (Exception e)
            {
                return false;
            }
        }

        public List<ApplicationModel> GetAppNameByUserId(int userId)
        {
            try
            {
                _appDAO = CreateSQLDAO();
                Dictionary<String, String> mapDictionary = new Dictionary<string, string>()
                {
                    {ApplicationModel.FIELD_ID,FIELD_ID},
                    {ApplicationModel.FIELD_APP_NAME,FIELD_APP_NAME},                                     
                };
                return _appDAO.GetAppNameByUserId(userId, mapDictionary);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public List<ApplicationModel> GetAppNameByGroupId(int? groupId)
        {
            try
            {
                _appDAO = CreateSQLDAO();
                Dictionary<String, String> mapDictionary = new Dictionary<string, string>()
                {
                    {ApplicationModel.FIELD_ID,FIELD_ID},
                    {ApplicationModel.FIELD_APP_NAME,FIELD_APP_NAME},                                     
                };
                return _appDAO.GetAppNameByGroupId(groupId, mapDictionary);
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
